home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Clock / Sources / Dialog.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  6.6 KB  |  227 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Dialog.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef DIALOG_H
  13. #include "Dialog.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. #ifndef DEFINES_K
  21. #include "Defines.k"
  22. #endif
  23.  
  24. #ifndef CONTENT_H
  25. #include "Content.h"
  26. #endif
  27.  
  28. // ----- ODF Includes -----
  29.  
  30. #ifndef FWEDVIEW_H
  31. #include "FWEdView.h"
  32. #endif
  33.  
  34. #ifndef FWNOTIFN_H
  35. #include "FWNotifn.h"
  36. #endif
  37.  
  38. #ifndef FWKEYF_H
  39. #include "FWKeyF.h"
  40. #endif
  41.  
  42. #ifndef FWRESOUR_H
  43. #include "FWResour.h"
  44. #endif
  45.  
  46. #ifndef FWALERT_H
  47. #include "FWAlert.h"
  48. #endif
  49.  
  50. #ifndef SLMIXOS_H
  51. #include "SLMixOS.h"    // for FW_Beep
  52. #endif
  53.  
  54. //========================================================================================
  55. // Runtime Information
  56. //========================================================================================
  57.  
  58. #ifdef FW_BUILD_MAC
  59. #pragma segment odfclock
  60. #endif
  61.  
  62. //========================================================================================
  63. // CClockSettingsDialogFrame class
  64. //========================================================================================
  65.  
  66. FW_DEFINE_AUTO(CClockSettingsDialogFrame)
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // CClockSettingsDialogFrame constructor
  70. //----------------------------------------------------------------------------------------
  71.  
  72. CClockSettingsDialogFrame::CClockSettingsDialogFrame(Environment* ev, 
  73.                 ODFrame* odFrame, 
  74.                 FW_CPresentation* presentation, 
  75.                 CClockPart* part)
  76. :    FW_CDialogFrame(ev, odFrame, presentation, part),
  77.     fClockPart(part),
  78.     fNumberView(NULL),
  79.     fStringView(NULL)
  80. {
  81.     FW_END_CONSTRUCTOR
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // CClockSettingsDialogFrame destructor
  86. //----------------------------------------------------------------------------------------
  87.  
  88. CClockSettingsDialogFrame::~CClockSettingsDialogFrame()
  89. {
  90.     FW_START_DESTRUCTOR
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // CClockSettingsDialogFrame::PostCreateViewFromStream
  95. //----------------------------------------------------------------------------------------
  96.  
  97. void CClockSettingsDialogFrame::PostCreateViewFromStream(Environment* ev)
  98. {
  99.     // ----- Call inherited to propagate to all subviews if necessary
  100.     FW_CSuperView::PostCreateViewFromStream(ev);
  101.  
  102.     // ----- Find the time offset edit view
  103.     FW_CView* view = FindViewByID(ev, kTimeOffsetViewID);
  104.     fNumberView = FW_DYNAMIC_CAST(FW_CEditView, view);
  105.     FW_ASSERT(fNumberView);
  106.  
  107.     FW_CString initialString;
  108.     CClockContent* clockContent = (CClockContent*) fClockPart->GetContent(ev);
  109.  
  110.     // ----- Initialize the time offset edit view
  111.     FW_CTimeSpan offset = clockContent->GetTimeOffset();
  112.     short hours = offset.GetHours();
  113.     initialString.ReplaceAllAsSignedDecimalInteger(hours);
  114.     fNumberView->SetText(ev, initialString);
  115.     CNumberFilter* filter = FW_NEW( CNumberFilter, (ev, (FW_MEventHandler*)fNumberView));
  116.  
  117.     // ----- Find the face string edit view
  118.     view = FindViewByID(ev, kFaceStringViewID);
  119.     fStringView = FW_DYNAMIC_CAST(FW_CEditView, view);
  120.     FW_ASSERT(fStringView);
  121.  
  122.     // ----- Initialize the face string edit view
  123.     initialString = clockContent->GetFaceString();
  124.     fStringView->SetText(ev, initialString);
  125.  
  126.     // WARNING: Make sure that classes created from resources won't be dead-stripped
  127.     FW_DO_NOT_DEAD_STRIP(FW_CButton);
  128.     FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
  129.     FW_DO_NOT_DEAD_STRIP(FW_CEditView);
  130. }
  131.  
  132.  
  133. //----------------------------------------------------------------------------------------
  134. // CClockSettingsDialogFrame::HandleNotification
  135. //----------------------------------------------------------------------------------------
  136.  
  137. void CClockSettingsDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  138. {
  139.     FW_Message msg = notification.GetMessage();
  140.  
  141.     // ---- Get new settings from edit fields when clicking on Set button ----
  142.     if (msg == FW_kDefaultButtonMsg) 
  143.     {
  144.         // Get new face string from EditView
  145.         FW_CString faceString = fStringView->GetText(ev);
  146.  
  147.         // Get offset string from EditView
  148.         FW_CString offsetString = fNumberView->GetText(ev);
  149.         long offset = offsetString.ParseAsSignedInteger();    // in hours
  150.         if (offset < -23 || offset > 23)    // Sanity check
  151.         {
  152.             // Open Alert and return to avoid closing the dialog
  153.             FW_CString partName;
  154.             FW_CString errorMessage;
  155.             FW_PSharedLibraryResourceFile resFile(ev);
  156.             ::FW_LoadStringByID(ev, resFile, kClockStrings, FW_kMultiStringRes, kOffsetErrorString, errorMessage);
  157.             fClockPart->GetPartName(ev, partName);
  158.             FW_ErrorAlert(partName, errorMessage);
  159.             return;
  160.         }
  161.  
  162.         // Pass new settings to part before closing dialog
  163.         fClockPart->SetClock(ev, offset, faceString);
  164.     }
  165.     
  166.     // Must call inherited to close the dialog on OK & Cancel
  167.     // WARNING: do not do anything else after this call... the dialog is gone!
  168.     FW_CDialogFrame::HandleNotification(ev, notification);
  169. }
  170.  
  171. //========================================================================================
  172. // CNumberFilter class
  173. //========================================================================================
  174.  
  175. FW_DEFINE_AUTO(CNumberFilter)
  176. FW_DEFINE_CLASS_M1(CNumberFilter, FW_MEventHandler)
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // CNumberFilter constructor
  180. //----------------------------------------------------------------------------------------
  181. CNumberFilter::CNumberFilter(Environment* ev, FW_MEventHandler* parent) 
  182.     : FW_MEventHandler()
  183. {
  184.     parent->AdoptEventHandler(ev, this);
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // CNumberFilter destructor
  189. //----------------------------------------------------------------------------------------
  190. CNumberFilter::~CNumberFilter() 
  191. {
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // CNumberFilter::DoCharKey
  196. //----------------------------------------------------------------------------------------
  197. FW_Handled CNumberFilter::DoCharKey(Environment* ev, const FW_CCharKeyEvent& keyEvent)
  198. {
  199.     char c = keyEvent.GetChar(ev);
  200.     
  201.     // Allow backspace and arrow keys, characters 0-9 and minus sign
  202.     switch (c)
  203.     {
  204.         case 0x08:
  205.         case 0x1C:
  206.         case 0x1D:
  207.         case 0x1E:
  208.         case 0x1F:
  209.         case '0':
  210.         case '1':
  211.         case '2':
  212.         case '3':
  213.         case '4':
  214.         case '5':
  215.         case '6':
  216.         case '7':
  217.         case '8':
  218.         case '9':
  219.         case '-':
  220.             return FW_kNotHandled;
  221.     }
  222.  
  223.     // not a valid character
  224.     FW_Beep();
  225.     return FW_kHandled;
  226. }
  227.